package service

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/core"
	"github.com/go-xorm/xorm"
	"kjjcgroup_server/config"
	"sync/atomic"
	"time"
)

var ConnStr = config.GetConnStr()

func GetEngine() (*xorm.Engine, error) {
	return xorm.NewEngine("mysql", ConnStr)
}

func GetEngBom() (*xorm.Engine, error) {
	conn, err := xorm.NewEngine("mysql", ConnStr)
	if err == nil {
		conn.SetMapper(core.SameMapper{})
		err2 := conn.Ping()
		if err2 != nil {
			//	fmt.Printf("err2 : %v\n", err2)
			fmt.Printf("\033[8;31;40m%s\033[0m\n", "数据库连接失败,请检查配置!")
		} else {
			print("数据库连接成功！")
		}
	}
	return conn, err
}

/*数据库时间定义*/
var _ID_CNT uint32 = 0
var _UNIX_START = time.Date(2012, 1, 1, 0, 0, 0, 0, time.Local).UnixNano() / 1e6

func GetUID() string {
	now := time.Now().UnixNano()/1e6 - _UNIX_START
	//now.Nanosecond()
	return fmt.Sprintf("%012X%04X", uint64(now), uint16(atomic.AddUint32(&_ID_CNT, 1)))
}
